package com.ljc.service;

import com.alibaba.fastjson.JSONArray;
import com.ljc.constant.RedisConstant;
import com.ljc.entity.UserMoment;
import com.ljc.interfaces.UserMomentApi;
import com.ljc.utils.MovementRabbitMQUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserMomentsService {
    @DubboReference
    private UserMomentApi userMomentApi;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private MovementRabbitMQUtils movementRabbitMQUtils;

    /**
     * 新建用户动态
     */
    public void addUserMoments(UserMoment userMoment) {
        userMoment.setCreateTime(new Date());
        userMoment = userMomentApi.addUserMoments(userMoment);
        movementRabbitMQUtils.sendMovementAsync(userMoment);
    }

    /**
     * 获得该用户的所有被推送的动态
     */
    public List<UserMoment> getUserSubscribedMoments(Long userId) {
        String key = RedisConstant.SUBSCRIBED_MOVEMENT_PREFIX + userId;
        String listStr = stringRedisTemplate.opsForValue().get(key);
        return JSONArray.parseArray(listStr, UserMoment.class);
    }
}
